package com.GlobalPaint.app.ui.Laborer;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.RecruitAdapter;
import com.GlobalPaint.app.Base.DialogWaiting;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.RecruitBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class RecruitFragment extends Fragment {
    private RecruitAdapter adapter;
    private int page = 1;
    private Dialog dlgWaiting2;
    private XRecyclerView recruit_xrecyler;
    private LinearLayoutManager linearLayoutManager;
    private RecruitBean bean;
    private List<RecruitBean.DataBean> list;
    private EditText ed_zhiwei;//地区
    private TextView tv_zousuo;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recruit, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        recruit_xrecyler = (XRecyclerView) view.findViewById(R.id.recruit_xrecyler);
        linearLayoutManager = new LinearLayoutManager(getContext());
        recruit_xrecyler.setLayoutManager(linearLayoutManager);
        ed_zhiwei = (EditText)view. findViewById(R.id.ed_zhiwei);
        tv_zousuo = (TextView)view. findViewById(R.id.tv_zousuo);
        tv_zousuo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(TextUtils.isEmpty(ed_zhiwei.getText().toString())){
                    getList();
                }else {
                    String zhiwei=ed_zhiwei.getText().toString();
                    sousuo(zhiwei);
                }
            }
        });
        getList();
        initLoad();
    }
    private void sousuo(String zhiwei){
        dlgWaiting2 = DialogWaiting.createDialogWaiting(getContext(), "加载中......");
        dlgWaiting2.show();
        String url= Constants.JASON_SERVICE_URL+"qlwjobhireinfo/query?page=1&location="+zhiwei;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), RecruitBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                bean = (RecruitBean) o;
                if (bean.getStatus() == 1) {
                    if (bean.getData() != null) {
                        list = bean.getData();
                        adapter = new RecruitAdapter(list, getContext());
                        recruit_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                    else
                    {
                        list=new ArrayList<RecruitBean.DataBean>();
                        adapter = new RecruitAdapter(list, getContext());
                        recruit_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                }
                recruit_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();
                if (list != null && list.size() > 0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    recruit_xrecyler.refreshComplete();

                }
            }
        });
    }
    private void initLoad() {
        recruit_xrecyler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url;
                if(TextUtils.isEmpty(ed_zhiwei.getText().toString())){
                    url = Constants.JASON_SERVICE_URL + "qlwjobhireinfo/query?page=" + 1;
                }else {
                    String zhiwei=ed_zhiwei.getText().toString();
                    url = Constants.JASON_SERVICE_URL + "qlwjobhireinfo/query?page=" + 1 +
                            "&location="+zhiwei;
                }
                DataManager.getInst().getHttpRequestJsonClass(url,DataManager.userEntity.getCookie(), RecruitBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        page = 1;
                        RecruitBean Jobbean = (RecruitBean) o;
                        if (Jobbean.getStatus() == 1) {
                            if (Jobbean.getData() != null && Jobbean.getData().size() > 0) {
                                list.clear();
                                list.addAll(Jobbean.getData());
                                adapter.notifyDataSetChanged();
                                recruit_xrecyler.loadMoreComplete();
                            }
                        }
                        recruit_xrecyler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        recruit_xrecyler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {
                page++;
                String url;
                if(TextUtils.isEmpty(ed_zhiwei.getText().toString())){
                    url = Constants.JASON_SERVICE_URL + "qlwjobhireinfo/query?page=" + page;
                }else {
                    String zhiwei=ed_zhiwei.getText().toString();
                    url = Constants.JASON_SERVICE_URL + "qlwjobhireinfo/query?page=" + page+
                            "&location="+zhiwei;
                }
                DataManager.getInst().getHttpRequestJsonClass(url,DataManager.userEntity.getCookie(), RecruitBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        RecruitBean jobBean = (RecruitBean) o;
                        if (jobBean.getStatus() == 1) {
                            if (jobBean.getData() != null && jobBean.getData().size() > 0) {
                                list.addAll(jobBean.getData());
                            }
                        } else {
                            page = page - 1;
                            Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        }
                        recruit_xrecyler.loadMoreComplete();
                        adapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        page = page - 1;
                        Toast.makeText(getContext(), "暂无更多数据", Toast.LENGTH_LONG).show();
                        recruit_xrecyler.loadMoreComplete();
                    }
                });
            }
        });
    }
    public void job(){
        String url = Constants.JASON_SERVICE_URL + "qlwjobhireinfo/query?page=1";
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), RecruitBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                if(list!=null)
                    list.clear();
                bean = (RecruitBean) o;
                if (bean.getStatus() == 1) {
                    if (bean.getData() != null) {
                        list = bean.getData();
                        adapter = new RecruitAdapter(list, getContext());
                        recruit_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                }
                recruit_xrecyler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

                if (list != null && list.size() > 0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    recruit_xrecyler.refreshComplete();

                }
            }
        });
    }
    private void getList() {
        dlgWaiting2 = DialogWaiting.createDialogWaiting(getContext(), "加载中......");
        dlgWaiting2.show();
        String url = Constants.JASON_SERVICE_URL + "qlwjobhireinfo/query?page=1";
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), RecruitBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {
                dlgWaiting2.cancel();
                bean = (RecruitBean) o;
                if (bean.getStatus() == 1) {
                    if (bean.getData() != null) {
                        list = bean.getData();
                        adapter = new RecruitAdapter(list, getContext());
                        recruit_xrecyler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                }
                recruit_xrecyler.refreshComplete();
                dlgWaiting2.cancel();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                dlgWaiting2.cancel();
                if (list != null && list.size() > 0) {
                    list.clear();
                    Toast.makeText(getContext(), "获取失败", Toast.LENGTH_LONG).show();
                    adapter.notifyDataSetChanged();
                    recruit_xrecyler.refreshComplete();

                }
            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
